package com.shujia.custom.udtf;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.util.ArrayList;

public class MyUDTFDemo2 extends GenericUDTF {

    /**
     * id, 时间, 值
     * a, 0时, 1
     * a, 2时, 2
     */
    @Override
    public StructObjectInspector initialize(StructObjectInspector argOIs) throws UDFArgumentException {
        ArrayList<String> nameList = new ArrayList<>();
        ArrayList<ObjectInspector> inspectorArrayList = new ArrayList<>();

        nameList.add("id");
        inspectorArrayList.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        nameList.add("time");
        inspectorArrayList.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        nameList.add("value");
        inspectorArrayList.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);

        return ObjectInspectorFactory.getStandardStructObjectInspector(nameList, inspectorArrayList);

    }

    @Override
    public void process(Object[] args) throws HiveException {
        String[] info = new String[3];
        int t = 0;
        //第一列是监测点编号
        info[0] = args[0].toString();
        for (int i = 1; i < args.length; i++) {
            info[1] = t + "时";
            info[2] = args[i].toString();
            forward(info);
            t += 2;
        }

    }

    @Override
    public void close() throws HiveException {

    }
}
